var rowid = RequestURLParam.getParam("rowid");
var type = RequestURLParam.getParam("type");
var parenttabID = RequestURLParam.getParam("tabID");
var urltext = "portalZoneView!add.do";
if (parenttabID) {
    parenttabID = decodeURIComponent(parenttabID);
}
var columns;
var portal;
$(document).ready(function () {
    minForm = new mini.Form("form");
    portal = new mini.ux.Portal();
    if (rowid && rowid.length > 10) {
        urltext = "portalZoneView!update.do";
        portal.render(document.getElementById("portalcontent"));
        ActHttpRequest("portalZoneView!findById.do", {id: rowid}, function (data) {
            if (data.OK == "OK") {
                if (data.List.length > 0) {
                    var portalZoneView = data.List[0];
                    $("#form .sysPortal").each(function () {
                        mini.get(this.id).setValue(portalZoneView[this.id]);
                    });
                    if (portalZoneView.portalZone && portalZoneView.portalZone.length > 0) {
                        columns = [];
                        var cols = portalZoneView.portalZone.split(",");
                        for (var i = 0; i < cols.length; i++) {
                            var col = cols[i].trim();
                            if (col.length > 0) {
                                columns.push(col);
                            }
                        }
                    } else {
                        columns = ["33%", "34%", "33%"];
                    }
                    portal.set({
                        style: "width:100%;height:100%",
                        columns: columns
                    });
                    var newpanels = [];
                    for (var i = 0; i < data.List.length; i++) {
                        var row = data.List[i];
                        newpanels.push({
                            column: parseInt(row.portalZonePosition),
                            id: row.portalModuleId,
                            title: row.portalModuleTitle,
                            showCloseButton: row.portalModuleIsclose == 1 ? true : false,
                            showCollapseButton: row.portalModuleCollapsible == 1 ? true : false,
                            showHeader: true,
                            showRefresh: row.portalModuleIsrefresh == 1 ? true : false,
                            width: "100%",
                            height: row.portalModuleHeight,
                            dblClick: true,
                            url: (row.portalModuleUrl.indexOf("http://") == 0 || row.portalModuleUrl.indexOf("https://") == 0 || row.portalModuleUrl.indexOf("../") == 0) ? row.portalModuleUrl : "../../" + row.portalModuleUrl
                        });
                    }
                    portal.setPanels(newpanels);
                } else {
                    columns = ["33%", "34%", "33%"];
                    portal.set({
                        style: "width:100%;height:100%",
                        columns: ["33%", "34%", "33%"]
                    });
                    portal.render(document.getElementById("portalcontent"));
                }
            } else {
                mini.alert(data.message);
            }
        });
    } else {
        columns = ["33%", "34%", "33%"];
        portal.set({
            style: "width:100%;height:100%",
            columns: ["33%", "34%", "33%"]
        });
        portal.render(document.getElementById("portalcontent"));
    }
});

function addPortalComponent() {
    mini.open({
        url: "base/dialog/selPortalModule.html?temp=" + new Date().getUTCMilliseconds(),
        title: "选择Portal模块", width: 1000, height: 500,
        onload: function () {
            //给对话框传参数！
            var iframe = this.getIFrameEl();
        },
        ondestroy: function (e) {
            if (e && e != "close" && e.length > 0) {
                var panels = [];
                for (var i = 0; i < e.length; i++) {
                    var row = e[i];
                    if ($("#" + row.portalModuleId).length > 0) {

                    } else {
                        if (mini.get("portalZone").getValue() != "") {
                            columns = [];
                            var cols = mini.get("portalZone").getValue().split(",");
                            for (var i = 0; i < cols.length; i++) {
                                var col = cols[i].trim();
                                if (col.length > 0) {
                                    columns.push(col);
                                }
                            }
                        } else {
                            columns = ["33%", "34%", "33%"];
                        }
                        portal.set({
                            style: "width:100%;height:100%",
                            columns: columns
                        });
                        panels.push({
                            column: 0,
                            id: row.portalModuleId,
                            title: row.portalModuleTitle,
                            showCloseButton: row.portalModuleIsclose == 1 ? true : false,
                            showCollapseButton: row.portalModuleCollapsible == 1 ? true : false,
                            showHeader: true,
                            showRefresh: row.portalModuleIsrefresh == 1 ? true : false,
                            width: "100%",
                            height: row.portalModuleHeight,
                            dblClick: true,
                            url: (row.portalModuleUrl.indexOf("http://") == 0 || row.portalModuleUrl.indexOf("https://") == 0 || row.portalModuleUrl.indexOf("../") == 0) ? row.portalModuleUrl : "../../" + row.portalModuleUrl
                        });
                    }
                }
                portal.setPanels(panels);
            }
        }
    });
}

function saveData() {
    minForm.validate();
    if (minForm.isValid() == false) {
        mini.alert("验证失败，点击页面红色叹号查看！");
        return;
    }
    var rows = [];
    var panels = portal.getPanels();
    var len = panels.length;

    for (var i = 0; i < len; i++) {
        var pannel = panels[i];
        console.log(pannel);
        if ($("#" + pannel.id).css("display") == "none") {
            continue;
        }
        rows.push({id: pannel.id, col: pannel.column, title: pannel.title});
    }
    var formData = $("#form").serializeJSON();
    console.log(rows);
    formData["portalZoneView.portalZones"] = mini.encode(rows);
    ActHttpRequest(urltext, formData, function (data) {
        if (data.OK == "OK") {
            if (parenttabID) {
                window.top.windowtab.execCallBackFun(parenttabID, "CallBackFun");
            }
            window.top.windowtab.CloseTab();
        } else {
            mini.alert(data.message);
        }
    }, null, null, null, '保存中。。。', true);
}